using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormUser : FormBase
    {
        private bool bCreateUser = false;
        private string UserName = "";

        public FormUser()
        {
            InitializeComponent();
            GetRoleList();
        }

        private void GetRoleList()
        {
            List<string> RoleList = doc_security.GetRoleList();
            foreach (string Role in RoleList)
            {
                cboUserRole.Items.Add(Role);
            }
        }

        public void ShowCreateUser()
        {
            bCreateUser = true;
            this.Text = "Create a new user";

            btnDeleteUser.Visible = false;

            this.ShowDialog();
        }

        public void ShowEditUser(string username)
        {
            bCreateUser = false;
            UserName = username;

            this.Text = "Edit User Information for [" + username + "]";

            System.Collections.Hashtable ht = doc_security.GetUser(username);

            if (ht.Count > 0)
            {
                LoadDataFromHashToControls(splitContainer1.Panel1.Controls, ht);

                this.ShowDialog();
            }
        }

        private void SaveUser()
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht = SaveDataToHashFromControls(splitContainer1.Panel1.Controls);

            if (bCreateUser)
            {
                ShowMessage(doc_security.CreateUser(ht));
            }
            else
            {
                ShowMessage(doc_security.UpdateUser(UserName, ht));
            }

            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveUser();
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (ConfirmToProcess("Are you sure you want to delete the user [" + UserName + "] ?"))
            {
                doc_security.DeleteUser(UserName);
                ShowMessage("User [" + UserName + "] removed.");

                this.Close();
            }
        }
    }
}